//
//  UIColor+Extesion.swift
//  Teamo
//
//  Created by mdzz on 2021/12/7.
//

import Foundation
import UIKit



extension UIColor {

    /// 不同level 不同的等级标签背景色
    /// - Parameter level: user's level
    /// - Returns: return the level background color
    public class func Color(level: Int) -> UIColor {
        var color = UIColor(red: 254, green: 175, blue: 12)
        if level > 3 {
            color = UIColor(red: 254, green: 85, blue: 69)
        }
        return color
   
    }
    
    public func alpha(alpha: CGFloat = 1.0) -> Self {
        
        return self
    }
    
    
    /// Create Color from RGB values with optional alpha.
    ///
    /// - Parameters:
    ///   - red: red component.
    ///   - green: green component.
    ///   - blue: blue component.
    ///   - alpha: optional alpha value (default is 1).
    public convenience init(red: Int, green: Int, blue: Int, alpha: CGFloat = 1) {
        var redComponent = red
        if red < 0 { redComponent = 0 }
        if red > 255 { redComponent = 255 }

        var greenComponent = green
        if green < 0 { greenComponent = 0 }
        if green > 255 { greenComponent = 255 }

        var blueComponent = blue
        if blue < 0 { blueComponent = 0 }
        if blue > 255 { blueComponent = 255 }

        var trans = alpha
        if trans < 0 { trans = 0 }
        if trans > 1 { trans = 1 }

        self.init(red: CGFloat(redComponent) / 255.0, green: CGFloat(greenComponent) / 255.0, blue: CGFloat(blueComponent) / 255.0, alpha: trans)
    }
    
    /// Hex String -> UIColor
    public convenience init(hexString: String) {
        let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
        let scanner = Scanner(string: hexString)
        
        if hexString.hasPrefix("#") {
            scanner.scanLocation = 1
        }
        var color: UInt32 = 0
        scanner.scanHexInt32(&color)
        let mask = 0x000000FF
        let r = Int(color >> 16) & mask
        let g = Int(color >> 8) & mask
        let b = Int(color) & mask
        
        let red = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue = CGFloat(b) / 255.0
        
        self.init(red: red, green: green, blue: blue, alpha: 1)
    }
}

